/**
 * ThreeJS帮助类
 * @constructor
 */
const ThreeHelper = function() {
	this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
	this.camera.position.set(-30, 30, 25);
	this.camera.lookAt(new THREE.Vector3(0, 0, 0));

	this.renderer = new THREE.WebGLRenderer({
		antialias: true,
		alpha: true
	});
	this.renderer.setSize(window.innerWidth, window.innerHeight);
	this.renderer.domElement.setAttribute('class', 'mainCanvas');
	document.body.appendChild(this.renderer.domElement);
	//  document.querySelector("#video").appendChild(this.renderer.domElement);

	this.scene = new THREE.Scene();
	this.scene.add(new THREE.AmbientLight(0xFFFFFF));

	const control = new THREE.OrbitControls(this.camera, this.renderer.domElement);
	control.update();

	this.clock = new THREE.Clock();
	this.mixers = [];

	window.addEventListener('resize', () => {
		this.camera.aspect = window.innerWidth / window.innerHeight;
		this.camera.updateProjectionMatrix();
		this.renderer.setSize(window.innerWidth, window.innerHeight);
	}, false);

	this.render = function() {
		this.renderer.render(this.scene, this.camera);

		for (const mixer of this.mixers) {
			mixer.update(this.clock.getDelta());
		}

		window.requestAnimationFrame(() => {
			this.render();
		});
	};

	this.loadObject = function(modelUrl) {


		const loader = new THREE.FBXLoader();
		loader.load(modelUrl, (object) => {
			//设置模型的初始缩放比例
			object.scale.setScalar(0.1);
			object.position.set(0, 0, 0);
			this.scene.add(object);
			//模型动画
			if (object.animations.length > 0) {
				object.mixer = new THREE.AnimationMixer(object);
				this.mixers.push(object.mixer);
				object.mixer.clipAction(object.animations[0]).play();
			}
		})
	};

	this.render();
};
